package org.smi;

import cn.hutool.setting.yaml.YamlUtil;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.InputStream;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Hello world!
 */
public class App {

    public static void main(String[] args) {

        Yaml yaml = new Yaml();

        InputStream configInS = App.class.getClassLoader().getResourceAsStream("config.yml");

        Map<String, Object> configMap = yaml.load(configInS);
//        String url2 = (String)configMap.get("url");
//        String name2 = (String)configMap.get("file.name");
//        Integer count2 = (Integer)configMap.get("count");

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
        String homeDir = System.getProperty("user.home");
        String before = (String)configMap.get("url");
        int b = (Integer)configMap.get("count");
        String after = ".ts";
        String saveDir = homeDir + (String)configMap.get("path") + ((String)configMap.get("fileName")).replace(" ", "_");
        boolean plusZero = false;
        int start = 0;
        int end = 0;
        try {
            int length = before.length();
            int tail = b + 3; // 3: .ts的长度
            int newLength = length - tail;
            String endStr = before.substring(newLength);

            before = before.substring(0, newLength);
            String[] endArr = endStr.split("\\.");
            end = Integer.parseInt(endArr[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }

        int count = end - start;
        CountDownLatch countDownLatch = new CountDownLatch(count);
        File dir = new File(saveDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        for (int i = start; i <= end; i++) {
            String tempUrl = "";
            String tempI = "";
            if (plusZero) {

                if (i < 10) {
                    tempI = "00" + i;
                } else if (i < 100) {
                    tempI = "0" + i;
                } else {
                    tempI = "" + i;
                }
            } else {
                tempI = "" + i;
            }
            String url = before + tempUrl + tempI + after;

            //String url = before + tempUrl + i + after;

            String temp = "";
            if (i < 10) {
                temp = "000";
            } else if (i < 100) {
                temp = "00";
            } else if (i < 1000) {
                temp = "0";
            } else {
                temp = "";
            }
            String fileName = "prefix" + temp + i + ".ts";

            System.out.println("启动下载 -> " + i);
            RunExec runExec = new RunExec();
            runExec.setUrl(url);
            runExec.setSaveDir(saveDir);
            runExec.setFileName(fileName);
            runExec.setId(i);
            runExec.setCountDownLatch(countDownLatch);

            fixedThreadPool.execute(runExec);

        }

        try {
            countDownLatch.await();
            System.out.println("【所有下载执行完毕】\n程序结束，再见！");
            System.exit(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }


}
